define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	var easyutil = require("resource/js/component/easyutil.js");
	var combobox = require("resource/js/component/easycombobox.js");
	var config = {
		type:"colssearch",
		name:"流程表字段",
		column:[[
		     {field:'colKey',title:'列编码',		width:100,align:'center',height:50,hidden:true},
	         {field:'tableKey',title:'表编码',	width:100,align:'center',height:50,hidden:true},
	         {field:'colName',title:'列名',		width:100,align:'center',height:50},
	         {field:'colNameCh',title:'说明信息',	width:100,align:'center',height:50},
	         {field:'colType',title:'类型',		width:100,align:'center',height:50},
	         {field:'colLength',title:'长度',		width:100,align:'center',height:50},
	         {field:'funName',title:'翻译函数',	width:100,align:'center',height:50},
	         {field:'isPk',title:'主键',			width:50,align:'center',height:50,hidden:true},
	         {field:'isPkText',title:'主键',		width:50,align:'center',height:50},
	         {field:'isFk',title:'外键',			width:50,align:'center',height:50,hidden:true},
	         {field:'isFkText',title:'外键',		width:50,align:'center',height:50},
	         {field:'fkTableKey',title:'外键表',	width:100,align:'center',height:50},
	         {field:'fkTableCol',title:'外键列',	width:100,align:'center',height:50},
	         {field:'showType',title:'展示类型',	width:100,align:'center',height:50,hidden:true},
	         {field:'showTypeText',title:'展示类型',width:100,align:'center',height:50},
	         {field:'dataId',title:'数据源类型',	width:100,align:'center',height:50},
	         {field:'dataSource',title:'数据源',		width:100,align:'center',height:50},
	         {field:'dataOnchange',title:'联动字段',width:100,align:'center',height:50},
	         {field:'isRequire',title:'是否必填',	width:100,align:'center',height:50,hidden:true},
	         {field:'isRequireText',title:'是否必填',width:100,align:'center',height:50},
	         {field:'checType',title:'校验类型',	width:100,align:'center',height:50,hidden:true},
	         {field:'checkTypeText',title:'校验类型',width:100,align:'center',height:50},
	         {field:'isProcess',title:'流程参数',width:80,align:'center',height:50,hidden:true},
	         {field:'isProcessText',title:'流程参数',width:80,align:'center',height:50},
	         {field:'isQueryCol',title:'查询参数',width:80,align:'center',height:50,hidden:true},
	         {field:'isQueryColText',title:'查询参数',width:80,align:'center',height:50},
	         {field:'isShowCol',title:'展示列',width:80,align:'center',height:50,hidden:true},
	         {field:'isShowColText',title:'展示列',width:80,align:'center',height:50},
	         {field:'operators',title:'条件运算符',width:80,align:'center',height:50},
	         {field:'orderNum',title:'排序',width:50,align:'center',height:50}
		]]
	};
	var businessId = undefined,tableKey = undefined;
	var dgutil = undefined;
	var datagridconfig = undefined;
	function initPage(){
		var urlTool = require("resource/js/component/urlTool.js");
		businessId = urlTool.getParam("businessId");
		tableKey = urlTool.getParam("tableKey");
		$("#cols_search_tableKey").val(tableKey);
		//初始化主表datagrid
		dgutil = new easyutil.ajaxDataGrid(datagridconfig);
	}
	var addDialogInit = function(baseUrl){
		$("#adddlg").dialog({
			iconCls: 'icon-save',
			title:config.name+'增加',
			href:baseUrl,
			onLoad:function(){
				$("#cols_add_tableKey").val($("#cols_search_tableKey").val());
				var flag = "add";
				using(["combobox","numberbox"],function(){
					easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"colType"},function(code,msg,data){
						combobox.init({ ele:"#colType_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onSelect:function(row){
							if(row.id=='VARCHAR2' || row.id=='VARCHAR'){
								$("#colLength_tr").find("td:last").html("");
								var colLenghtInput = "<input type='text' class='easyui-numberbox' name='colLength' value='0' data-options=\"required:true,min:0,precision:0,prompt:'请输入字段长度'\"/>";
								$("#colLength_tr").find("td:last").append(colLenghtInput);
								$(".easyui-numberbox").numberbox();
								$("#colLength_tr").show();
							}/*else if(row.id=='NUMBER'){
								$("#colLength_tr").find("td:last").html("");
								var colLenghtInput = "<input class='easyui-numberbox' type='text' name='colLength' value='0.00' data-options=\"required:true,min:0,precision:2,prompt:'请输入字段长度'\"/>"
								$("#colLength_tr").find("td:last").append(colLenghtInput);
								$(".easyui-numberbox").numberbox();
								$("#colLength_tr").show();
							}*/else{
								$("#colLength_tr").find("td:last").html("");
								$("#colLength_tr").hide();
							}
						}}).load(data);
					});
					easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"showType"},function(code,msg,data){
						combobox.init({ ele:"#showType_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onSelect:function(row){
							if(row.id=='select' || row.id=='mselect' || row.id=='tree' || row.id == 'mtree'){
								var showTypeInput1="<input class=\"easyui-textbox\" type=\"text\" name=\"dataId\" data-options=\"required:true,validType:'null',prompt:'请输入数据源类型'\"/>";
								var showTypeInput2="<input class=\"easyui-textbox\" type=\"text\" name=\"dataSource\" data-options=\"required:true,validType:'null',prompt:'请输入数据源'\"/>";
								$("#dataId_tr").find("td:last").html(showTypeInput1);
								$("#dataSource_tr").find("td:last").html(showTypeInput2);
								$(".easyui-textbox").textbox();
								$("#dataId_tr").show();
								$('#dataSource_tr').show();
								$("#dataOnchange_tr").show();
							}else if(row.id == 'books'){
								var booksAttr1="<input  name=\"dataId\" id=\"dataId\" />";
								var booksAttr2="<input  name=\"dataSource\" id=\"dataSource\" />";
								$("#dataId_tr").find("td:last").html(booksAttr1);
								$("#dataSource_tr").find("td:last").html(booksAttr2);
								$('#dataId').combobox({
									 valueField:'id',    
									 textField:'text',
									 data:[{
										 id:"checkbox",
										 text:"多选"
									 },{
										id:"radio",
										text:"单选"
									 }],
									 prompt:"请选择勾选类型",
									 editable:false
									 
								});
								$('#dataSource').combobox({
									 valueField:'id',    
									 textField:'text',
									 data:[{
										 id:"user",
										 text:"人员"
									 },{
										id:"dept",
										text:"部门"
									 },{
										 id:"role",
										 text:"职务"
									 }],
									 prompt:"请选择数据类型",
									 editable:false
								});
								$("#dataId_tr").show();
								$('#dataSource_tr').show();
							}else{
								$("#dataId_tr").find("td:last").html("");
								$("#dataSource_tr").find("td:last").html("");
								$("#dataId_tr").hide();
								$('#dataSource_tr').hide();
								$("#dataOnchange_tr").hide();
							}
						}}).load(data);
					});
					easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"checkType"},function(code,msg,data){
						combobox.init({ ele:"#checkType_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false}).load(data);
					});
					easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"itemiskey"},function(code,msg,data){
						combobox.init({ ele:"#isProcess_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onLoadSuccess:function(){
								$("#isProcess_"+flag).combobox("setValue",1);
							}
						}).load(data);
						combobox.init({ ele:"#isQueryCol_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onLoadSuccess:function(){
							$("#isQueryCol_"+flag).combobox("setValue",1);
						}}).load(data);
						combobox.init({ ele:"#isShowCol_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onLoadSuccess:function(){
							$("#isShowCol_"+flag).combobox("setValue",1);
						}}).load(data);
						combobox.init({ ele:"#isRequire_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onLoadSuccess:function(){
							$("#isRequire_"+flag).combobox("setValue",1);
						}}).load(data);
						combobox.init({ ele:"#isPk_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onLoadSuccess:function(){
							$("#isPk_"+flag).combobox("setValue",1);
						}}).load(data);
						combobox.init({ ele:"#isFk_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onLoadSuccess:function(){
							$("#isFk_"+flag).combobox("setValue",1);
						},onSelect:function(rec){
							if(rec.id=='0'){
								//初始化外键input
								var fkTableKeyInput = "<input class=\"easyui-combobox\" type=\"text\" id='fkTableKey' name=\"fkTableKey\" data-options=\"required:true,validType:'combobox',prompt:'请选择外键表名'\"/>";
								$("#fkTableKey_tr").find("td:last").append(fkTableKeyInput);
								$("#fkTableKey_tr").show();
								var fkColsInput = "<input class=\"easyui-combobox\" type=\"text\" id='fkTableCol' name=\"fkTableCol\" data-options=\"required:true,validType:'combobox',prompt:'请选择外键列名'\"/>";
								$("#fkTableCol_tr").find("td:last").append(fkColsInput);
								$("#fkTableCol_tr").show();
								//初始化外键combobox
								easyutil.ajaxHandler(BASEPATH+'/oa/act/actExTable/queryTableListByBuss.htm',{businessId:businessId,tableKey:tableKey},function(code,msg,data){
									combobox.init({ ele:"#fkTableKey",valueField:'id',remote:false, textField:'name',emptyval:false,onSelect:function(rec){debugger;
										for(var i in data){
											if(data[i].short_name_ch==rec.short_name_ch){
												rec.id = data[i].id;
												break;
											}
										}
										easyutil.ajaxHandler(BASEPATH+'/oa/act/actExTableCols/queryActExTableCols.htm',{tableKey:rec.id},function(code,msg,data){
											var cols = [];
											for(var i in data.rows){
												var name = data.rows[i].colName+"("+data.rows[i].colNameCh+")";
												var row = {id:data.rows[i].colName,name:name};
												cols.push(row);
											}
											combobox.init({ ele:"#fkTableCol",valueField:'id',remote:false, textField:'name',emptyval:false}).load(cols);
										});
									}}).load(data);
								});
							}else{
								$("#fkTableKey_tr").find("td:last").html("");
								$("#fkTableKey_tr").hide();
								$("#fkTableCol_tr").find("td:last").html("");
								$("#fkTableCol_tr").hide();
							}
						}}).load(data);
						easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"operators"},function(code,msg,data){
							combobox.init({ ele:"#operators_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onLoadSuccess:function(data){
								$("#operators_"+flag).combobox('select','=');
							}}).load(data);
						});
					});
				});
			},
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#addform").form("validate")){
							easyutil.ajaxHandler(BASEPATH+'/oa/act/actExTableCols/saveActExTableCols.htm',$("#addform").serializeJson(),function(){
								dgutil.pager();
								$("#adddlg").dialog("close");
							});
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#adddlg').dialog('close');
				}
			}]
		});
		$("#adddlg").dialog("open");
	}
	var updDialogInit = function(baseUrl,row){
		$("#adddlg").dialog({
			iconCls: 'icon-save',
			title:config.name+'修改',
			href:baseUrl,
			onLoad:function(){
				//初始化表单
				if(row.colType=='INTEGER'){
					$("#colLength_tr").find("td:last").html("");
					$("#colLength_tr").hide();
				}
				if(row.showType=='hidden' || row.showType=='text' || row.showType=='integer' || row.showType=='number' || row.showType=='textarea'){
					$("#dataId_tr").find("td:last").html("");
					$("#dataId_tr").hide();
					$("#dataSource_tr").find("td:last").html("");
					$("#dataSource_tr").hide();
					$("#dataOnchange_tr").hide();
				}
				if(row.isFk=='1'){
					$("#fkTableKey_tr").find("td:last").html("");
					$("#fkTableKey_tr").hide();
					$("#fkTableCol_tr").find("td:last").html("");
					$("#fkTableCol_tr").hide();
				}
				using(["form"],function(){
					var flag = "upd";
					using(["combobox","numberbox"],function(){
						easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"colType"},function(code,msg,data){
							combobox.init({ ele:"#colType_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onSelect:function(row){
								if(row.id=='VARCHAR2' || row.id=='VARCHAR'){
									$("#colLength_tr").find("td:last").html("");
									var colLenghtInput = "<input type='text' class='easyui-numberbox' name='colLength' value='0' data-options=\"required:true,min:0,precision:0,prompt:'请输入字段长度'\"/>";
									$("#colLength_tr").find("td:last").append(colLenghtInput);
									$(".easyui-numberbox").numberbox();
									$("#colLength_tr").show();
								}/*else if(row.id=='NUMBER'){
									$("#colLength_tr").find("td:last").html("");
									var colLenghtInput = "<input class='easyui-numberbox' type='text' name='colLength' value='0.00' data-options=\"required:true,min:0,precision:2,prompt:'请输入字段长度'\"/>"
									$("#colLength_tr").find("td:last").append(colLenghtInput);
									$(".easyui-numberbox").numberbox();
									$("#colLength_tr").show();
								}*/else{
									$("#colLength_tr").find("td:last").html("");
									$("#colLength_tr").hide();
								}
							}}).load(data);
						});
						easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"showType"},function(code,msg,data){
							combobox.init({ ele:"#showType_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onSelect:function(row){
								if(row.id=='select' || row.id=='mselect' || row.id=='tree' || row.id == 'mtree'){
									var showTypeInput1="<input class=\"easyui-textbox\" type=\"text\" name=\"dataId\" data-options=\"required:true,validType:'null',prompt:'请输入数据源类型'\"/>";
									var showTypeInput2="<input class=\"easyui-textbox\" type=\"text\" name=\"dataSource\" data-options=\"required:true,validType:'null',prompt:'请输入数据源'\"/>";
									$("#dataId_tr").find("td:last").html(showTypeInput1);
									$("#dataSource_tr").find("td:last").html(showTypeInput2);
									$(".easyui-textbox").textbox();
									$("#dataId_tr").show();
									$('#dataSource').show();
									$("#dataOnchange_tr").show();
								}else if(row.id == 'books'){
									var booksAttr1="<input  name=\"dataId\" id=\"dataId\" />";
									var booksAttr2="<input  name=\"dataSource\" id=\"dataSource\" />";
									$("#dataId_tr").find("td:last").html(booksAttr1);
									$("#dataSource_tr").find("td:last").html(booksAttr2);
									$('#dataId').combobox({
										 valueField:'id',    
										 textField:'text',
										 data:[{
											 id:"checkbox",
											 text:"多选"
										 },{
											id:"radio",
											text:"单选"
										 }],
										 prompt:"请选择勾选类型",
										 editable:false
										 
									});
									$('#dataSource').combobox({
										 valueField:'id',    
										 textField:'text',
										 data:[{
											 id:"user",
											 text:"人员"
										 },{
											id:"dept",
											text:"部门"
										 },{
											 id:"role",
											 text:"职务"
										 }],
										 prompt:"请选择数据类型",
										 editable:false
									});
									$("#dataId_tr").show();
									$('#dataSource_tr').show();
								}else{
									$("#dataId_tr").find("td:last").html("");
									$("#dataSource_tr").find("td:last").html("");
									$("#dataId_tr").hide();
									$('#dataSource').hide();
									$("#dataOnchange_tr").hide();
								}
							}}).load(data);
						});
						easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"checkType"},function(code,msg,data){
							combobox.init({ ele:"#checkType_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false}).load(data);
						});
						easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"itemiskey"},function(code,msg,data){
							combobox.init({ ele:"#isProcess_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false}).load(data);
							combobox.init({ ele:"#isQueryCol_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false}).load(data);
							combobox.init({ ele:"#isShowCol_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false}).load(data);
							combobox.init({ ele:"#isPk_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false}).load(data);
							combobox.init({ ele:"#isRequire_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false}).load(data);
							combobox.init({ ele:"#isFk_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false,onSelect:function(rec){
								if(rec.id=='0'){
									//初始化外键input
									var fkTableKeyInput = "<input class=\"easyui-combobox\" type=\"text\" id='fkTableKey' name=\"fkTableKey\" data-options=\"required:true,validType:'combobox',prompt:'请选择外键表名'\"/>";
									$("#fkTableKey_tr").find("td:last").append(fkTableKeyInput);
									$("#fkTableKey_tr").show();
									var fkColsInput = "<input class=\"easyui-combobox\" type=\"text\" id='fkTableCol' name=\"fkTableCol\" data-options=\"required:true,validType:'combobox',prompt:'请选择外键列名'\"/>";
									$("#fkTableCol_tr").find("td:last").append(fkColsInput);
									$("#fkTableCol_tr").show();
									//初始化外键combobox
									easyutil.ajaxHandler(BASEPATH+'/oa/act/actExTable/queryTableListByBuss.htm',{businessId:businessId,tableKey:tableKey},function(code,msg,data){
										combobox.init({ ele:"#fkTableKey",valueField:'id',remote:false, textField:'name',emptyval:false,onSelect:function(rec){
											easyutil.ajaxHandler(BASEPATH+'/oa/act/actExTableCols/queryActExTableCols.htm',{tableKey:rec.id},function(code,msg,data){
												console.log(data);
												var cols = [];
												for(var i in data.rows){
													var name = data.rows[i].colName+"("+data.rows[i].colNameCh+")";
													var row = {id:data.rows[i].colKey,name:name};
													cols.push(row);
												}
												combobox.init({ ele:"#fkTableCol",valueField:'id',remote:false, textField:'name',emptyval:false}).load(cols);
											});
										}}).load(data);
									});
								}else{
									$("#fkTableKey_tr").find("td:last").html("");
									$("#fkTableKey_tr").hide();
									$("#fkTableCol_tr").find("td:last").html("");
									$("#fkTableCol_tr").hide();
								}
							}}).load(data);
						});
						easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"operators"},function(code,msg,data){
							combobox.init({ ele:"#operators_"+flag,valueField:'id',remote:false, textField:'text',emptyval:false}).load(data);
						});
					});
					setTimeout(function(){
						$('#updform').form('load',row);
					},200);
				});
			},
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#updform").form("validate")){
							easyutil.ajaxHandler(BASEPATH+'/oa/act/actExTableCols/updateActExTableCols.htm',$("#updform").serializeJson(),function(){
								dgutil.pager();
								$("#adddlg").dialog("close");
							});
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#adddlg').dialog('close');
				}
			}]
		});
		$("#adddlg").dialog("open");
	}
	
	var delInit = function(baseUrl,rows){
		var checks = "";
		var newsContentOldPath = "";
		for(var key in rows){
			if(key==rows.length-1){
				checks += rows[key].colKey;
			}else{
				checks += rows[key].colKey+",";
			}
		}
		easyutil.ajaxHandler(baseUrl,{checkeds:checks},function(){
			dgutil.delRow(rows);
		});
	}
	module.exports = {
		trans:function(id){
			var buttons ={ 
				title:{
					_class:"",
					text:"字段配置列表"
				},
				btn:[
			       {
				iconCls: 'icon-search',
				url:'/oa/act/actExTableCols/queryActExTableCols.htm',
				text:"查询",
				//addClass:'l-btn-display',
				handler: function(){dgutil.pager(1);}
			},{
				iconCls: 'icon-add',
				//addClass:'l-btn-display',
				text:"添加",
				url:'/view/oa/act/ex/cols/add',
				handler: addDialogInit
			},
			 {
				iconCls: 'icon-edit',
				url:'/view/oa/act/ex/cols/upd',
				text:"修改",
				//addClass:'l-btn-display',
				needRow:true,
				handler: updDialogInit
			},
			 {
				iconCls: 'icon-remove',
				url:'/oa/act/actExTableCols/deleteActExTableCols.htm',
				text:"删除",
				//addClass:'l-btn-display',
				needRows:true,
				isCheck:true,
				handler: delInit
			}
			]};
			datagridconfig = {
					fitColumns:false,
					singleSelect:true,
					striped:true,
					autoRowHeight:false,
					fit: true,
					url:BASEPATH+'/oa/act/actExTableCols/queryActExTableCols.htm',
					toolbar:buttons,
					columns:config.column,
					eid:"dg2",
					args:"searchform2",
					pagesize:50
			};
			using(["datagrid","parser","textbox","dialog","combobox","messager"],function(){
				initPage();
			});
			
		}
	}
});